Problem Statement #

Given a set of positive numbers, find if we can partition it into two subsets such that the sum of elements in both subsets is equal.

Example 1:

Input: {1, 2, 3, 4}
Output: True
Explanation: The given set can be partitioned into two subsets with equal sum: {1, 4} & {2, 3}

Example 2:

Input: {1, 1, 3, 4, 7}
Output: True
Explanation: The given set can be partitioned into two subsets with equal sum: {1, 3, 4} & {1, 7}

Example 3:

Input: {2, 3, 4, 6}
Output: False
Explanation: The given set cannot be partitioned into two subsets with equal sum.

Try it yourself #

This problem looks similar to the 0/1 Knapsack problem. Try solving it before moving on to see the solution:

--NORMAL--

Output

0.640s

Can partition: False Can partition: False Can partition: False

Basic Solution #

This problem follows the 0/1 Knapsack pattern. A basic brute-force solution could be to try all combinations of partitioning the given numbers into two sets to see if any pair of sets has an equal sum.

Assume that S represents the total sum of all the given numbers. Then the two equal subsets must have a sum equal to S/2. This essentially transforms our problem to: "Find a subset of the given numbers that has a total sum of S/2".

So our brute-force algorithm will look like:

--NORMAL--

Code #

Here is the code for the brute-force solution:

--NORMAL--

Output

0.484s

Can partition: True Can partition: True Can partition: False

Time and Space complexity #

The time complexity of the above algorithm is exponential O(2n)O(2^n), where ‘n’ represents the total number. The space complexity is O(n)O(n), which will be used to store the recursion stack.

Top-down Dynamic Programming with Memoization #

We can use memoization to overcome the overlapping sub-problems. As stated in previous lessons, memoization is when we store the results of all the previously solved sub-problems so we can return the results from memory if we encounter a problem that has already been solved.

Since we need to store the results for every subset and for every possible sum, therefore we will be using a two-dimensional array to store the results of the solved sub-problems. The first dimension of the array will represent different subsets and the second dimension will represent different ‘sums’ that we can calculate from each subset. These two dimensions of the array can also be inferred from the two changing values (sum and currentIndex) in our recursive function canPartitionRecursive().

Code #

Here is the code for Top-down DP with memoization:

--NORMAL--

Output

0.468s

Can partition: True Can partition: True Can partition: False

Time and Space complexity #

The above algorithm has the time and space complexity of O(NS)O(N*S), where ‘N’ represents total numbers and ‘S’ is the total sum of all the numbers.

Bottom-up Dynamic Programming #

Let’s try to populate our dp[][] array from the above solution by working in a bottom-up fashion. Essentially, we want to find if we can make all possible sums with every subset. This means, dp[i][s] will be ‘true’ if we can make the sum ‘s’ from the first ‘i’ numbers.

So, for each number at index ‘i’ (0 <= i < num.length) and sum ‘s’ (0 <= s <= S/2), we have two options:

  1. Exclude the number. In this case, we will see if we can get ‘s’ from the subset excluding this number: dp[i-1][s]
  2. Include the number if its value is not more than ‘s’. In this case, we will see if we can find a subset to get the remaining sum: dp[i-1][s-num[i]]

If either of the two above scenarios is true, we can find a subset of numbers with a sum equal to ‘s’.

Let’s start with our base case of zero capacity:

Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 2 3 4 5
'0' sum can always be found through an empty set
1 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T 2 F 3 F 4 F 5 F
With only one number, we can form a subset only when the required sum is equal to its value
2 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T T 2 F 3 F 4 F 5 F
sum: 1, index:1=> (dp[index-1][sum] , as the 'sum' is less than the number at index '1'
3 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T T 2 F T 3 F 4 F 5 F
sum: 2, index:1=> (dp[index-1][sum] || dp[index-1][sum-2])
4 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T T 2 F T 3 F T 4 F 5 F
sum: 3, index:1=> (dp[index-1][sum] || dp[index-1][sum-2])
5 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T T 2 F T 3 F T 4 F F 5 F F
sum: 4,5, index:1=> (dp[index-1][sum] || dp[index-1][sum-2])
6 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T T T 2 F T T 3 F T T 4 F F 5 F F
sum: 1,2,3, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])
7 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T T T 2 F T T 3 F T T 4 F F T 5 F F
sum: 4, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])
8 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T T T 2 F T T 3 F T T 4 F F T 5 F F T
sum: 5, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])
9 of 10
Created with Fabric.js 1.6.0-rc.1 num\sum 1 {1, 2} {1,2,3} {1,2,3,4} 0 T T T T 1 T T T T 2 F T T T 3 F T T T 4 F F T T 5 F F T T
sum: 1-5, index:3=> (dp[index-1][sum] || dp[index-1][sum-4])
10 of 10

From the above visualization, we can clearly see that it is possible to partition the given set into two subsets with equal sums, as shown by bottom-right cell: dp[3][5] => T

Code #

Here is the code for our bottom-up dynamic programming approach:

--NORMAL--

Output

0.473s

Can partition: True Can partition: True Can partition: False

Time and Space complexity #

The above solution the has time and space complexity of O(NS)O(N*S), where ‘N’ represents total numbers and ‘S’ is the total sum of all the numbers.

Mark as Completed
←    Back
0/1 Knapsack (medium)
Next    →
Subset Sum (medium)